package com.cskaoyan.controller;


import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.Role;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.RoleService;
import com.cskaoyan.shiro.MallToken;
import com.cskaoyan.vo.AdminVo;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.RoleVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;

@RestController
public class AdminController {

    @Autowired
    AdminService adminService;
    @Autowired
    RoleService roleService;
    @RequestMapping("admin/admin/list")
    public BaseRespVo list(Integer page, Integer limit,String sort,String order,String username){
        if(username == null) {
            AdminVo admins = adminService.list(page, limit, sort, order);
            return BaseRespVo.ok(admins);
        }
        AdminVo admins = adminService.list(page, limit, sort, order,username);
        return BaseRespVo.ok(admins);
    }

    @RequestMapping("admin/admin/update")
    public BaseRespVo update(@RequestBody Admin admin){
        adminService.update(admin);
        return new BaseRespVo().ok();
    }

    @RequestMapping("admin/admin/delete")
    public BaseRespVo delete(@RequestBody Admin admin){
        adminService.delete(admin);
        return new BaseRespVo().ok();
    }

    @RequestMapping("admin/role/list")
    public BaseRespVo roleList(Integer page, Integer limit,String sort,String order,String name){
        if(name != null ) {
//            System.out.println(name);
            RoleVo roles = roleService.list(page, limit, sort, order, name);
            return BaseRespVo.ok(roles);
        }
        RoleVo roles = roleService.list(page, limit, sort, order);
        return BaseRespVo.ok(roles);
    }

    @RequestMapping("admin/role/update")
    public BaseRespVo update(@RequestBody Role role){
        System.out.println(role);
        roleService.update(role);
        return new BaseRespVo();
    }

    @RequestMapping("admin/role/delete")
    public BaseRespVo delete(@RequestBody Role role){
        roleService.delete(role);
        return new BaseRespVo();
    }

    @RequestMapping("admin/role/create")
    public BaseRespVo create(@RequestBody Role role){
        Role role1 = roleService.create(role);
        return new BaseRespVo().ok(role1);
    }

    @RequestMapping("/admin/admin/create")
    public BaseRespVo create(@RequestBody Admin admin){
        Admin admin1 = adminService.create(admin);
        return new BaseRespVo().ok(admin1);
    }

//    @RequestMapping("admin/role/permissions")
//    public BaseRespVo peimissions(){
//       PermissionsVo permissionsVo =  adminService.permissions();
//       return  new BaseRespVo().ok(permissionsVo);
//
//    }


    @PostMapping("admin/auth/login")
    public BaseRespVo login(@RequestBody Map map, HttpServletRequest request) throws Exception {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        MallToken mallToken = new MallToken(username, password, "admin");
        Subject subject = SecurityUtils.getSubject();
        //将登录信息交给认证器
        subject.login(mallToken);
        //获取sessionId 并返回
        Serializable id = subject.getSession().getId();

        String ip = request.getRemoteAddr();
        boolean b = adminService.insertLogin(ip);
        return BaseRespVo.ok(id);
    }
}
